﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class PlacementDAO : IDAO<Placement>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(Placement entity)
        {
            agrDb.Placements1.AddObject(entity);
        }

        public void remove(Placement entity)
        {
            agrDb.Placements1.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (Placement placement in agrDb.Placements1.Where(s => s.PlacementCode == code))
            {
                agrDb.Placements1.DeleteObject(placement);
            }
        }

        public void merge(Placement entity)
        {
            foreach (Placement placement in agrDb.Placements1.Where(s => s.PlacementCode == entity.PlacementCode))
            {
                placement.PlacementType = entity.PlacementType;
            }

            //Select ALL
            //var placements = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public Placement findById(string code)
        {
            Placement placement = new Placement();

            foreach (Placement place in agrDb.Placements1.Where(s => s.PlacementCode == code))
            {
                placement = place;
            }

            return placement;
        }

        public IEnumerable<Placement> findAll()
        {
            var placements = (from place in agrDb.Placements1
                            select place);

            return placements;
        }

        public IEnumerable<Placement> findInRange(int firstResult, int maxResuslts)
        {
            var placements = (from place in agrDb.Placements1
                            select place);

            return placements.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var placements = (from place in agrDb.Placements1
                            select place).ToList();

            return placements.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}